package com.sdp.khy.baotou.controller;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.khy.baotou.db.khy_package.entity.KhyPackage;
import com.sdp.khy.baotou.db.khy_package.service.IKhyPackageService;
import com.sdp.khy.baotou.db.khy_package_logistics.entity.KhyPackageLogistics;
import com.sdp.khy.baotou.db.khy_package_logistics.service.IKhyPackageLogisticsService;
import com.sdp.khy.baotou.db.khy_transport.entity.KhyTransport;
import com.sdp.khy.baotou.db.khy_transport.service.IKhyTransportService;
import com.sdp.khy.baotou.utils.KhyConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/KhyTransportController")
public class KhyTransportController {

	@Autowired
	private IKhyTransportService iKhyTransportService;
	
	@Autowired
	private IKhyPackageService iKhyPackageService;
	
	@Autowired
	private IKhyPackageLogisticsService iKhyPackageLogisticsService;
	
	@Autowired
	private Query query;

	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iKhyTransportService, KhyTransport.class, dataCenter));
	}
	
	/**
	 * 生成运输单，理货员将快递与司机和车辆绑定
	 * @param data
	 * @return
	 */
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody JSONObject data) {
		if(data==null) {
			return Result.error("运输信息为空");
		}
	
		KhyTransport transport = data.getObject("transport", KhyTransport.class);
		List<KhyPackage> packageList = JSONObject.parseArray(JSON.toJSONString(data.get("packageList")), KhyPackage.class);
		if(transport==null || packageList==null || packageList.size()==0) {
			return Result.error("运输信息为空");
		}
		
		transport.setCreateTime(LocalDateTime.now());
		transport.setTransportState(KhyConstants.运输单状态_已揽收.getCode());
		iKhyTransportService.save(transport);
		
		//快递运输信息
		List<KhyPackageLogistics> packageLogisticsList = new ArrayList<>();
		KhyPackageLogistics packageLogistics = null;
		
		for(KhyPackage p : packageList) {
			p.setTransportId(transport.getKeyId());
			p.setPlateNo(transport.getPlateNo());
			p.setDriverNo(transport.getDriverNo());
			p.setPackageState(KhyConstants.快递状态_已揽收.getCode());
			
			
			packageLogistics = new KhyPackageLogistics();
			packageLogistics.setLogisticsDesc("已揽收");
			packageLogistics.setLogisticsTime(LocalDateTime.now());
			packageLogistics.setPackageId(p.getKeyId());
			packageLogisticsList.add(packageLogistics);
		}
		iKhyPackageService.updateBatchById(packageList);
		iKhyPackageLogisticsService.saveBatch(packageLogisticsList);
		return Result.ok(transport);
	}
}

